package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.system.domain.Employee;
import com.ruoyi.system.domain.vo.DutyInformationVo;
import com.ruoyi.system.service.IEmployeeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DutyInformation;
import com.ruoyi.system.service.IDutyInformationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 值班信息Controller
 * 
 * @author 管鸿影
 * @date 2024-12-08
 */
@Controller
@RequestMapping("/system/information")
public class DutyInformationController extends BaseController
{
    private String prefix = "system/information";

    @Resource
    private IEmployeeService employeeService;

    @Autowired
    private IDutyInformationService dutyInformationService;

//    @RequiresPermissions("system:information:view")
    @GetMapping()
    public String information()
    {
        return prefix + "/information";
    }

    /**
     * 查询值班信息列表
     */
//    @RequiresPermissions("system:information:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DutyInformation dutyInformation)
    {
        startPage();
        List<DutyInformation> list = dutyInformationService.selectDutyInformationList(dutyInformation);
        List<DutyInformationVo> dutyInformationVos = new ArrayList<>();
        list.forEach(df ->{
            Employee employee = employeeService.selectEmployeeByEmployeeId(df.getEmployeeId());
            DutyInformationVo dutyInformationVo = new DutyInformationVo();
            BeanUtils.copyProperties(df,dutyInformationVo);
            dutyInformationVo.setEmployee(Optional.ofNullable(employee).map(Employee::getName).orElse("关联员工已被删除"));
            dutyInformationVos.add(dutyInformationVo);
        });

        return getDataTable(dutyInformationVos);
    }

    /**
     * 导出值班信息列表
     */
    @RequiresPermissions("system:information:export")
    @Log(title = "值班信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DutyInformation dutyInformation)
    {
        List<DutyInformation> list = dutyInformationService.selectDutyInformationList(dutyInformation);
        ExcelUtil<DutyInformation> util = new ExcelUtil<DutyInformation>(DutyInformation.class);
        return util.exportExcel(list, "值班信息数据");
    }

    /**
     * 新增值班信息
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<Employee> employees = employeeService.selectEmployeeList(new Employee());
        modelMap.put("employees",employees);
        return prefix + "/add";
    }

    /**
     * 新增保存值班信息
     */
    @RequiresPermissions("system:information:add")
    @Log(title = "值班信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DutyInformation dutyInformation)
    {
        return toAjax(dutyInformationService.insertDutyInformation(dutyInformation));
    }

    /**
     * 修改值班信息
     */
    @RequiresPermissions("system:information:edit")
    @GetMapping("/edit/{dutyId}")
    public String edit(@PathVariable("dutyId") Long dutyId, ModelMap mmap)
    {
        DutyInformation dutyInformation = dutyInformationService.selectDutyInformationByDutyId(dutyId);
        mmap.put("dutyInformation", dutyInformation);
        List<Employee> employees = employeeService.selectEmployeeList(new Employee());
        mmap.put("employees",employees);
        return prefix + "/edit";
    }

    /**
     * 修改保存值班信息
     */
    @RequiresPermissions("system:information:edit")
    @Log(title = "值班信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DutyInformation dutyInformation)
    {
        return toAjax(dutyInformationService.updateDutyInformation(dutyInformation));
    }

    /**
     * 删除值班信息
     */
    @RequiresPermissions("system:information:remove")
    @Log(title = "值班信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dutyInformationService.deleteDutyInformationByDutyIds(ids));
    }
}
